/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.quantity;

import org.openaecfm.model.quantity.PhysicalComplexQuantity;
import org.openaecfm.model.quantity.PhysicalQuantity;
import org.openaecfm.service.quantity.IPhysicalComplexQuantityManager;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class PhysicalComplexQuantityIO extends
		AbstractEntityInstanceInputImpl<PhysicalComplexQuantity> implements
		IEntityInstanceOutput<PhysicalComplexQuantity> {
	private IEntityInstanceUtilInput<PhysicalQuantity> utilInput;
	private IEntityInstanceUtilOutput<PhysicalQuantity> utilOutput;
	private IPhysicalComplexQuantityManager manager;

	public void setManager(IPhysicalComplexQuantityManager manager) {
		this.manager = manager;
	}

	public void setUtilOutput(
			IEntityInstanceUtilOutput<PhysicalQuantity> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(PhysicalComplexQuantity pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCPHYSICALCOMPLEXQUANTITY", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set HasQuantities
		ListParameter hasqs = new ListParameter();
		for (PhysicalQuantity pq : pojo.getHasQuantities()) {
			EntityInstanceNameParameter n = new EntityInstanceNameParameter(
					appDataManager.getName(pq));
			hasqs.add(n);
		}
		ei.addParameter(hasqs);

		// Set Discrimination
		StringParameter discri = new StringParameter(pojo.getDiscrimination(),
				StringParameter.UTF_8);
		ei.addParameter(discri);

		// Set Quality
		if (pojo.getQuality() == null) {
			DollarParameter d = DollarParameter.getInstance();
			ei.addParameter(d);
		} else {
			StringParameter s = new StringParameter(pojo.getQuality(),
					StringParameter.UTF_8);
			ei.addParameter(s);
		}

		// Set Usage
		if (pojo.getUsage() == null) {
			DollarParameter d = DollarParameter.getInstance();
			ei.addParameter(d);
		} else {
			StringParameter s = new StringParameter(pojo.getUsage(),
					StringParameter.UTF_8);
			ei.addParameter(s);
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected PhysicalComplexQuantity doCreateObject() {
		PhysicalComplexQuantity pojo = new PhysicalComplexQuantity();

		return pojo;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			PhysicalComplexQuantity pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set HasQuantities
		ListParameter hasqs = (ListParameter) sei.getParameter(2);
		for (Parameter p : hasqs) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) p;
			manager.addHasQuantity(pojo, (PhysicalQuantity) appDataManager
					.getObject(n.toInstanceName()));
		}
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<PhysicalQuantity> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			PhysicalComplexQuantity pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Discrimination
		StringParameter discri = (StringParameter) sei.getParameter(3);
		pojo.setDiscrimination(discri.toUTF8());

		// Set Quality
		Parameter quality = sei.getParameter(4);
		if (quality.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) quality;
			pojo.setQuality(s.toUTF8());
		}

		// Set Usage
		Parameter usage = sei.getParameter(5);
		if (usage.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) usage;
			pojo.setUsage(s.toUTF8());
		}
	}
}
